<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\TradingInfo;
use App\Admin\Repositories\TradingHall;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class TradingHallController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TradingHall(['user']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.name', '会员名');
            $grid->column('user.flag', '会员标签');
            $grid->column('type', '类型')->using([
                1 => '买入',
                2 => '卖出',
            ]);

            $grid->column('amount');
            $grid->column('price');
            $grid->column('traded_quantity');
            $grid->column('status', '状态')->using([
                1 => '交易中',
                2 => '交易完成',
                3 => '取消交易',
            ]);
            $grid->column('info', '明细')->display(function () {
                if ($this->type == 1) {
                    $msg = '卖出者明细';
                } else {
                    $msg = '买入者明细';
                }
                return $msg;
            })
                ->expand(function () {
                    return (new TradingInfo())->payload(['id' => $this->id, 'type' => $this->type]);
                });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.name', '会员名');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TradingHall(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('amount');
            $show->field('price');
            $show->field('traded_quantity');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TradingHall(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('type');
            $form->text('amount');
            $form->text('price');
            $form->text('traded_quantity');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
